<?php	

	class Common
	{
	
		var $link;
		var $log;

		public function __construct()
		{
			$this->link = $this->initConnection();	
		}		

		private function initConnection()
		{
			$link =  mysql_connect(DB_HOST,DB_TEST_USER,DB_TEST_PASS);
			if( !$link )
			{
				die('Not Connected '. mysql_error());
			}
			return $link;
		}

		public function connectDB()
		{
			$db_selected = mysql_select_db(DB_TEST,$this->link);
			// set UTF8
			mysql_query("set names 'utf8'", $this->link);
			
			if( !$db_selected )
			{
				die('Can\'t  use db '. mysql_error());
			}
			return $db_selected;
		}				
		
		/**
	     * slug()
	     * creates a slug from a string
	     */
	    public function slug($phrase) 
	    {
	        $find = array('á','à','ả','ã','ạ');
	        $phrase = str_replace($find, 'a', $phrase);
			
			$find = array('â','ấ','ầ','ẩ','ẫ','ậ');
	        $phrase = str_replace($find, 'a', $phrase);
	
	        $find = array('í','ì','ỉ','ĩ','ị');
	        $phrase = str_replace($find, 'i', $phrase);
	
	        $find = array('é','è','ẻ','ẽ','ẹ');
	        $phrase = str_replace($find, 'e', $phrase);
	
	        $find = array('ê','ế','ề','ể','ễ','ệ');
	        $phrase = str_replace($find, 'e', $phrase);
	
	        $find = array('ý','ỳ','ỷ','ỹ','ỵ');
	        $phrase = str_replace($find, 'y', $phrase);
	
	        $find = array('ó','ò','ỏ','õ','ọ');
	        $phrase = str_replace($find, 'o', $phrase);     
	
	        $find = array('ô','ố','ồ','ổ','ỗ','ộ');
	        $phrase = str_replace($find, 'o', $phrase);     
	
	        $find = array('ơ','ớ','ờ','ở','ỡ','ợ');
	        $phrase = str_replace($find, 'o', $phrase);     
	
	        $find = array('ú','ù','ủ','ũ','ụ');
	        $phrase = str_replace($find, 'u', $phrase);             
	
	        $find = array('ư','ứ','ừ','ử','ữ','ự');
	        $phrase = str_replace($find, 'u', $phrase);             
	
			//adding special case
			//đ
			$find = array('đ','Đ');
			$phrase = str_replace($find, 'd', $phrase); 
	
	        $str = strtolower($phrase);
	
	        // replace spaces with underscore, all to lowercase
	        $str = strtolower(str_replace(' ', '_', $str));
	
	        // create regex pattern
	        $pattern = "/[^a-zA-Z0-9_]/";
	
	        // replace non alphanumeric characters
	        $str = preg_replace($pattern, '', $str);
	
	        return $str;
	    }

	    function addLog($str)
	    {
	    	$this->log .= $str ."<br/>";
	    }

	    function getLog()
	    {
	    	return $this->log;
	    }
	}
